/*
 * User Model
 * @Author: chiangbt@geoportal.cn 
 * @Date: 2019-05-07 12:56:52 
 * @Last Modified by: chiangbt@geoportal.cn
 * @Last Modified time: 2019-05-19 12:33:33
 */
const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');

const userSchema = mongoose.Schema({
    // id
    _id: mongoose.Schema.Types.ObjectId,
    // 用户名,值唯一
    username: {
        type: String,
        required: true,
        unique: true
    },
    // email唯一
    email: {
        type: String,
        required: true,
        unique: true,
        match: /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/
    },
    // password设置
    password: {
        type: String,
        required: true
    },
    // 是否活跃
    isActive: {
        type: Boolean,
        default: true
    },
    // 角色
    role: {
        type: String,
        required: true,
        default: 'Guest',
    },
}, {
    versionKey: false, // 避免出现_v字段
    timestamps: { // 自动生成createdAt和updatedAt字段
        createdAt: 'createdAt',
        updatedAt: 'updatedAt'
    }
});

userSchema.plugin(uniqueValidator);
module.exports = mongoose.model('User', userSchema);